﻿import { Component, ElementRef, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {  ComboboxItemDto,  CreateOrUpdateOrderDto, OrderServiceProxy, LineInstanceServiceProxy, AddRespondInput } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import * as moment from 'moment';
import { from } from 'rxjs';

@Component({
    selector: 'respondModal',
    templateUrl: './respond-modal.component.html'
})
export class RespondModalComponent extends AppComponentBase {

    @ViewChild('createOrEditModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;

    gotoOrderIndex = 1;
    processExeList :ComboboxItemDto[] = [];

    requestId = 0;
    instanceId = '';

    respondInfo ='';

    constructor(
        injector: Injector,
        private _lineInstanceServiceProxy: LineInstanceServiceProxy
    ) {
        super(injector);
    }

    show(requestId?: number,instanceId?:string): void {    

        this.requestId = requestId;
        this.instanceId = instanceId;
        this.active = true;        
        this.modal.show();     
        this._lineInstanceServiceProxy.getProcessOfExeForCombobox(instanceId)
        .subscribe((result)=>
        {
            this.processExeList = result;

            this.processExeList.forEach(element => {
               if(element.isSelected)
                {
                    this.gotoOrderIndex = Number(element.value) ;
                }
           });
        })

    }

    save(): void {         
        
        let respond = new AddRespondInput();
        respond.gotoIndex = this.gotoOrderIndex;
        respond.instanceId =  this.instanceId;
        respond.requestId = this.requestId;
        respond.respondInfo = this.respondInfo;

        this._lineInstanceServiceProxy.addRespond(respond).subscribe(() => 
        {
            this.notify.info(this.l('SavedSuccessfully'));
            this.close();
            this.modalSave.emit(null);
        });            
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
